#coding=utf-8
'''
Created on 2014年1月27日

@author: fanfzj
'''
import os
from data_manage.data_option import DataOptionGet

path = os.getcwd()
def joke():
    os.popen('cd '+path)
    os.popen('scrapy crawl jokespider.org')
def china():
    os.popen('cd '+path)
    os.popen('scrapy crawl chinaspider.org')
    os.popen('scrapy crawl chinacontentspider.org')
    dataoptionget = DataOptionGet()
    dataoptionget.lock_Links_CH()
def internation():
    os.popen('cd '+path)
    os.popen('scrapy crawl interspider.org')
    os.popen('scrapy crawl intercontentspider.org')
    dataoptionget = DataOptionGet()
    dataoptionget.lock_Links_IN()
def sport():
    os.popen('cd '+path)
    os.popen('scrapy crawl sportspider.org')
    os.popen('scrapy crawl sportcontentspider.org')
    dataoptionget = DataOptionGet()
    dataoptionget.lock_Links_Sport()
def malitary():
    os.popen('cd '+path)
    os.popen('scrapy crawl malispider.org')
    os.popen('scrapy crawl malicontentspider.org')
    dataoptionget = DataOptionGet()
    dataoptionget.lock_Links_MA()
def community():
    os.popen('cd '+path)
    os.popen('scrapy crawl communityspider.org')
    os.popen('scrapy crawl communitycontentspider.org')
    dataoptionget = DataOptionGet()
    dataoptionget.lock_Links_CO()
def entertainment():
    os.popen('cd '+path)
    os.popen('scrapy crawl entertainmentspider.org')
    os.popen('scrapy crawl entertainmentcontentspider.org')
    dataoptionget = DataOptionGet()
    dataoptionget.lock_Links_ET()

def terminal():
    dataoptionget = DataOptionGet()
    print 'data mining start...'
    print 'joke china internation sports'
    print 'malitary community entertainment'
    while True:
        command = raw_input('input command:').strip().rstrip().split(' ')
        if command[0] == 'exit':
            break
        if not command[0] == 'runcrawler' and not command[0] == 'show' and not command[0] == 'shownum':
            print 'command error'
        elif command[0] == 'runcrawler':
            if command[1] == 'all':
                joke()
                china()
                internation()
                sport()
                malitary()
                community()
                entertainment()
            else:
                for i in range(len(command)):
                    if command[i] == 'joke':
                        joke()
                    if command[i] == 'china':
                        china()
                    if command[i] == 'internation':
                        internation()
                    if command[i] == 'sports':
                        sport()
                    if command[i] == 'malitary':
                        malitary()
                    if command[i] == 'community':
                        community()
                    if command[i] == 'entertainment':
                        entertainment()
        elif command[0] == 'show':
            for i in range(len(command)):
                    if command[i] == 'china':
                        mission = dataoptionget.get_table_message('china')
                        for j in range(len(mission)):
                            print str(j)+'.'+mission[j].title
                    if command[i] == 'internation':
                        mission = dataoptionget.get_table_message('internation')
                        for j in range(len(mission)):
                            print str(j)+'.'+mission[j].title
                    if command[i] == 'sports':
                        mission = dataoptionget.get_table_message('sport')
                        for j in range(len(mission)):
                            print str(j)+'.'+mission[j].title
                    if command[i] == 'malitary':
                        mission = dataoptionget.get_table_message('malitary')
                        for j in range(len(mission)):
                            print str(j)+'.'+mission[j].title
                    if command[i] == 'community':
                        mission = dataoptionget.get_table_message('community')
                        for j in range(len(mission)):
                            print str(j)+'.'+mission[j].title
                    if command[i] == 'entertainment':
                        mission = dataoptionget.get_table_message('entertainment')
                        for j in range(len(mission)):
                            print str(j)+'.'+mission[j].title
        elif command[0] == 'shownum':
            if command[1] == 'all':
                print 'joke:'+str(dataoptionget.get_title_num('joke'))
                print 'china:'+str(dataoptionget.get_title_num('china'))
                print 'internation:'+str(dataoptionget.get_title_num('internation'))
                print 'sports:'+str(dataoptionget.get_title_num('sports'))
                print 'malitary:'+str(dataoptionget.get_title_num('malitary'))
                print 'community:'+str(dataoptionget.get_title_num('community'))
                print 'entertainment:'+str(dataoptionget.get_title_num('entertainment'))
            else:
                for i in range(len(command)):
                    if command[i] == 'joke':
                        print 'joke:'+str(dataoptionget.get_title_num('joke'))
                    if command[i] == 'china':
                        print 'china:'+str(dataoptionget.get_title_num('china'))
                    if command[i] == 'internation':
                        print 'internation:'+str(dataoptionget.get_title_num('internation'))
                    if command[i] == 'sports':
                        print 'sports:'+str(dataoptionget.get_title_num('sports'))
                    if command[i] == 'malitary':
                        print 'malitary:'+str(dataoptionget.get_title_num('malitary'))
                    if command[i] == 'community':
                        print 'community:'+str(dataoptionget.get_title_num('community'))
                    if command[i] == 'entertainment':
                        print 'entertainment:'+str(dataoptionget.get_title_num('entertainment'))

if __name__ == '__main__':
    terminal()
